package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.*;
import DAO.*;
import service.*;
import servlet.*;


/**
 * 用户Servlet，处理用户登录，用户注册等
 * @author Administrator
 *
 */
public class UserServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public UserServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//获取前台参数，根据参数选择对应的servlet方法进行业务处理
		String op = request.getParameter("op");
		
		if("register".equals(op)){//用户注册
			registerService(request,response);
		}else if("select".equals(op)){//查看用户
			service(request,response);
		}
	}

	/**
	 * 用户注册
	 * @param request
	 * @param response
	 
	private void registerService(HttpServletRequest request,
			HttpServletResponse response) {
		
		//获取前台传递的三个参数，其中id在数据中使用自增字段，无需手动输入
		String name=request.getParameter("name");
		String password=request.getParameter("password");
		String password2 = request.getParameter("password2");
		
		User user =null;
		if(password.equals(password2)) {
			user = new User(name,password);
		}else {
			try {
				response.sendRedirect("register.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		IUserDao dao = DaoFactory.getUserDao();
		try{
		if(dao.insert(user)){
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}else{
			response.sendRedirect("register.jsp");
		}}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}
	*/
	
	private void registerService(HttpServletRequest request, HttpServletResponse response) {
		// TODO 自动生成的方法存根
		
	}

	/**
	 * 查询用户
	 * @param request
	 * @param response
	 
	private void selectService(HttpServletRequest request,
			HttpServletResponse response) {
		
		  IUserDao dao = DaoFactory.getUserDao();

		  try{
		  if(dao.queryUser()!=null){
			  request.getRequestDispatcher("user.jsp").forward(request, response);
		  }else{
			  response.sendRedirect("main.jsp");
		  }
		  }
		  catch(Exception e){
			  e.printStackTrace();
		  }
	}
	*/

	public void init() throws ServletException {
	}

}
